package studentInterface;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;

public class LogIn extends JFrame implements ActionListener{

	int width;
	int height;
	JButton bntAccept, bntCancel;
	JLabel lblPassword, lblUserName;
	JTextField tfUsername;
	JPasswordField tfPassword;
	private void initialInterface() throws Exception
	{
		UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		Box box, box1, box2, box3, box4, box5;
		setTitle("Đăng nhập");
		width = 500;
		height = 200;
		setSize(width, height);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		bntAccept = new JButton("Chấp nhận");
		bntCancel = new JButton(" Hủy bỏ ");
		lblUserName = new JLabel("Mã sinh viên");
		lblPassword = new JLabel("Mật khẩu");
		
		tfUsername = new JTextField();
		tfPassword = new JPasswordField();
		box = Box.createVerticalBox();
		box1 = Box.createHorizontalBox();
		box2 = Box.createHorizontalBox();
		box3 = Box.createHorizontalBox();
		box4 = Box.createHorizontalBox();
		box5 = Box.createHorizontalBox();
		
		
		box1.add(Box.createHorizontalStrut(30));
		box1.add(lblUserName);
		box1.add(Box.createHorizontalStrut(30));
		box1.add(tfUsername);
		box1.add(Box.createHorizontalStrut(30));
		box2.add(Box.createHorizontalStrut(30));
		box2.add(lblPassword);
		box2.add(Box.createHorizontalStrut(44));
		box2.add(tfPassword);
		box2.add(Box.createHorizontalStrut(30));
		
		box3.add(Box.createHorizontalStrut(20));
		box3.add(bntAccept);
		box3.add(bntCancel);
		box.add(Box.createVerticalStrut(40));
		box.add(box1);
		box.add(Box.createVerticalStrut(20));
		box.add(box2);
		box.add(Box.createVerticalStrut(30));
		box.add(box3);
		box.add(Box.createVerticalStrut(40));
		
		add(box);
		bntAccept.addActionListener(this);
		bntCancel.addActionListener(this);
	}
	public LogIn() throws Exception{
		initialInterface();
	}

	public static void main(String[] args) {
		try {
			new LogIn().setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == bntAccept)
		{
			char[] _password = tfPassword.getPassword();
			String _username = tfUsername.getText();
			if(isUserCorrect(_username, _password))
			{
				JOptionPane.showMessageDialog(this, "Đăng nhập thành công");
				setVisible(false);
				try {
					new UserInterface().setVisible(true);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			//Zero out the password.
		    Arrays.fill(_password,'0');
		}
		if(e.getSource() == bntCancel)
		{
			System.exit(1);
		}
	}
	
	private boolean isUserCorrect(String user, char[] pass) {
	    boolean isCorrect = true;
	    char[] correctPassword = { '1', '1', '1', '1', '1', '1'};

	    if (pass.length != correctPassword.length) {
	        isCorrect = false;
	    } else {
	        isCorrect = Arrays.equals (pass, correctPassword);
	    }

	    //Zero out the password.
	    Arrays.fill(correctPassword,'0');

	    return isCorrect;
	}

}
